﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace HIWSystem.Helpers
{
    public class XMLHelper
    {
        static  XDocument xDoc = null;
        private static void LoadXML(string uri)
        {
            try
            {
               xDoc= XDocument.Load(uri);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public static DataTable GetAllChildNodeByAttribute(string path,string rootName,string parentNode,string searchNode,string attributeKey,string attributeValue)
        {
            DataTable dt = new DataTable();
            DataRow dataRow = null;
            try
            {
                LoadXML(path);
                var query = from c in xDoc?.Element(rootName).Elements(searchNode)
                            where c.Attribute(attributeKey).Value == attributeValue
                            select c.Element(searchNode).Elements();
                            
                if (query.Count()>0)
                {
                    foreach (XElement e in query)
                    {
                        dt.Columns.Add(e.Name.ToString());
                    }
                    dataRow = dt.NewRow();
                    foreach (XElement e in query)
                    {
                        dataRow[e.Name.ToString()] = e.Value;
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return dt;
        }
        /// <summary>
        /// 获取节点元素
        /// </summary>
        /// <param name="path">路径</param>
        /// <param name="serarchNodeName">查询的节点元素名称</param>
        /// <returns></returns>
        public static IEnumerable<XElement> GetNodeElements(string path, string serarchNodeName)
        {
            IEnumerable<XElement> elements = new List<XElement>();
            try
            {
                LoadXML(path);
                elements = xDoc.Descendants(serarchNodeName);
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("获取XML节点元素时发生错误【{0}】",ex.Message));
            }
            return elements;
        }
        /// <summary>
        /// 返回指定节点的值
        /// </summary>
        /// <typeparam name="T">返回值类型</typeparam>
        /// <param name="path">XML保存的路径</param>
        /// <param name="searchNote">查询的节点名</param>
        /// <returns></returns>
        public static T GetNoteValue<T>(string path,string searchNote)
        {
            var value = default(T);
            LoadXML(path);
            try
            {
               value=(T)Convert.ChangeType(xDoc.Descendants(searchNote).FirstOrDefault().Value,typeof(T));
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("获取XML节点值的时候发生错误"+ex.Message));
            }
            return value;
        }
        /// <summary>
        /// 获取节点的属性值
        /// </summary>
        /// <typeparam name="T">属性值类型</typeparam>
        /// <param name="path">XML的路径</param>
        /// <param name="searchNoteName">查询的节点名</param>
        /// <param name="attributeName">属性名</param>
        /// <returns></returns>
        public static IEnumerable<T> GetNoteAttributeValue<T>(string path, string searchNoteName, string attributeName)
        {
            var lstAttributeValues = new List<T>();
            try
            {
                LoadXML(path);
                lstAttributeValues = xDoc.Descendants(searchNoteName)?.Select(e=>(T)Convert.ChangeType( e.Attribute(attributeName).Value,typeof(T))).ToList();
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("获取XML节点的属性值时发生错误【{0}】",ex.Message));
            }
            return lstAttributeValues;
        }
    }
}
